package edu.nf.exam.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.entity.SubjectInfo;
import edu.nf.exam.service.SubjectService;
import edu.nf.exam.service.impl.SubjectServiceImpl;
import edu.nf.exam.vo.ResultVO;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import java.util.List;

/**
 * @Author : QYM
 * @create 2021/6/20
 */
public class SubjectController extends BaseController{
    /**
     * 添加学科
     * @param subjectInfo
     * @return
     */
    @RequestMapping("/subject/add")
    public View AddSubject(SubjectInfo subjectInfo){
        //创建业务实现类对象
        SubjectService service = new SubjectServiceImpl();
        //调用查询方法（城市列表查询）
        try {
            service.checkAddSubject(subjectInfo);
            //返回JsonView视图
            return new JsonView(success());
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500, e.getMessage()));
        }

    }

    /**
     * 删除学科
     * @param subjectId
     * @return
     */
    @RequestMapping("/subject/del")
    public View DelSubject(Integer subjectId){
        //创建业务实现类对象
        SubjectService service = new SubjectServiceImpl();
        //调用查询方法（城市列表查询）
        try {
            service.checkDelSubject(subjectId);
            //返回JsonView视图
            return new JsonView(success());
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 修改学科
     * @param subjectInfo
     * @return
     */
    @RequestMapping("/subject/update")
    public View UpdateSubject(SubjectInfo subjectInfo){
        //创建业务实现类对象
        SubjectService service = new SubjectServiceImpl();
        //调用查询方法（城市列表查询）
        try {
            service.checkUpdateSubject(subjectInfo);
            //返回JsonView视图
            return new JsonView(success());
        } catch (Exception e) {

            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 模糊查询学科
     * @param levelName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/subject/queryByLike")
    public View QueryByLikeSubject(String levelName, Integer page, Integer limit){
        //System.out.println(levelName);
        //创建业务实现类对象
        SubjectService service = new SubjectServiceImpl();
        //调用查询方法
        PageInfo<SubjectInfo> pageInfo = service.checkQueryByLikeSubject(levelName,page,limit);
        //实现数据响应并分页
        ResultVO<List<SubjectInfo>> vo = page(pageInfo);
        //返回JsonView视图
        return new JsonView(vo);
    }

    /**
     * 根据ID查询学科信息
     * @param subjectId
     * @return
     */
    @RequestMapping("/subject/queryById")
    public View QueryByIdSubject(Integer subjectId){
        SubjectService service = new SubjectServiceImpl();
        try {
            SubjectInfo subject = service.checkQueryById(subjectId);
            return new JsonView(success(subject));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 去重查询年级
     * @return
     */
    @RequestMapping("/subject/selectLevelNameByDistinct")
    public View selectLevelNameByDistinct(){
        SubjectService service = new SubjectServiceImpl();
        List<SubjectInfo> list = service.selectLevelNameByDistinct();
        return new JsonView(success(list));
    }

    /**
     * 多条件查询
     * @return
     */
    @RequestMapping("/subject/selectByMore")
    public View selectByMore(SubjectInfo subjectInfo){
        SubjectService service = new SubjectServiceImpl();
        List<SubjectInfo> list = service.selectByMore(subjectInfo);
        return new JsonView(success(list));
    }

    /**
     * 根据学科名称和年级查询，服务于判断学科创编和修改是否唯一
     * @param subjectName
     * @param levelName
     * @return
     */
    @RequestMapping("/list/subject/name")
    public View queryByNamesubject(String subjectName, String levelName){
        try {
            SubjectService service = new SubjectServiceImpl();
            service.checkSubject(subjectName, levelName);
            return new JsonView(success());
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

}
